/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.crawlerextractor.crawler4j.frontier;

import org.apache.log4j.Logger;

import com.googlecode.crawlerextractor.crawler4j.crawler.Configurable;
import com.googlecode.crawlerextractor.crawler4j.crawler.CrawlConfig;
import com.googlecode.crawlerextractor.crawler4j.service.DocIdService;
import com.googlecode.crawlerextractor.crawler4j.url.DocId;


/**
 * @author Yasser Ganjisaffar <lastname at gmail dot com>
 */

public class DocIDServer extends Configurable {

	protected static final Logger logger = Logger.getLogger(DocIDServer.class.getName());

	protected DocIdService docIdService=DocIdService.getInstance();


	protected final Object mutex = new Object();



	public DocIDServer( final CrawlConfig config)  {
		super(config);

		if (config.isResumableCrawling()) {

		} else {

		}
	}

	public void addUrlAndDocId(final String url, final int docId) throws Exception {



		// Make sure that we have not already use  a docid
		final DocId prevId = docIdService.findById(docId);
		if (prevId!=null) {
			throw new Exception("Requested doc id: " + docId + " is alredy used for url  larger than: " + prevId.getUrl());
		}
		// Make sure that we have not already assigned a docid for this URL
		final DocId prevDocid = docIdService.findByUrl(url);
		if (prevDocid!=null) {
			if (prevDocid.getId() == docId) {
				return;
			}
			throw new Exception("Doc id: " + prevDocid.getId() + " is already assigned to URL: " + url);
		}
		final DocId newDocId= new DocId(docId,url);
		docIdService.persist(newDocId);

	}






	/**
	 * Returns the docid of an already seen url.
	 *
	 * @param url the URL for which the docid is returned.
	 * @return the docid of the url if it is seen before. Otherwise -1 is returned.
	 */
	public int getDocId(final String url) {


		final DocId docId=docIdService.findByUrl(url);
		if(docId==null){
			return -1;
		}
		return		docId.getId();



	}

	public int getNewDocID(final String url) {
		int docid ;
		synchronized (mutex) {

			// Make sure that we have not already assigned a docid for this URL
			docid = this.getDocId(url);
			if (docid > 0) {
				return docid;
			}

			final DocId newDocId= new DocId(url);
			docIdService.persist(newDocId);
			docid= newDocId.getId();



		}
		return docid;
	}

	public boolean isSeenBefore(final String url) {
		return this.getDocId(url) != -1;
	}


}
